#include "MultiMediaRegister.h"

bool sortByName(MultiMedia &media1, MultiMedia &media2)
{
	return media1.getName() < media2.getName();
}

bool sortByYear(MultiMedia &media1, MultiMedia &media2)
{
	return media1.getYear() < media2.getYear();
}

MultiMediaRegister::MultiMediaRegister()
{
}


MultiMediaRegister::~MultiMediaRegister()
{
}

void MultiMediaRegister::addMedia(int year, string name)
{
	v.push_back(MultiMedia(year, name));
}

void MultiMediaRegister::removeMedia(int index)
{
}

//vector<MultiMedia>::iterator MultiMediaRegister::findMedia(int year, string name)
//{
//	return find(v.begin(), v.end(), MultiMedia(year, name));
//}

void MultiMediaRegister::saveToFile(string file)
{
	ofstream fout;
	fout.open(file);
	for (int i = 0; i < v.size(); i++){
		fout << v[i];
	}
	fout.close();
}

void MultiMediaRegister::loadFromFile(string file)
{
	ifstream fin;
	fin.open(file);
	string strRow;
	while (std::getline(fin, strRow)){
		istringstream iss(strRow);
		string name;
		int year;
		getline(iss, name, ';');
		iss >> year;
		v.push_back(MultiMedia(year, name));
	}
}

void MultiMediaRegister::sortRegister(string opt)
{
	if (opt == "year"){
		sort(v.begin(), v.end(), sortByYear);
	}
	else if (opt == "name"){
		sort(v.begin(), v.end(), sortByName);
	}
}

void MultiMediaRegister::print()
{
	for (int i = 0; i < v.size(); i++){
		cout << v[i];
	}
}